﻿using System;
using System.Linq;
using System.Web;
using System.IO;
using db = DataContext;

namespace WebApplication.admin
{
	public partial class products : System.Web.UI.Page
	{
		public bool isadd, isdelete, issave;

		public int start, limit;

		public int id = 0;
		public string file_src = "";

		protected void Page_Load(object sender, EventArgs e)
		{
			Common.CheckLogin();

			isadd = Common.IsEdit;
			isdelete = Common.IsDelete;
			issave = Common.IsSave;

			start = Common.Start;
			limit = p_pager.Limit;

			id = Common.Id;

			using (db.DbDataContext context = new db.DbDataContext())
			{
				if (isadd)
				{
					type.DataSource = context.ProductType;
					type.DataBind();
					if (id > 0)
					{
						db.Product product = context.Product.Single(x => x.Id == id);
						name.Value = product.Name;
						intro.Value = product.Description;
						if (product.Pictures.Count > 0)
							file_src = "/" + product.Pictures.OrderBy(x => x.Id).First().GUID + ".ashx";
						type.Value = product.Type.ToString();
					}
				}

				if (issave)
				{
					if (id > 0)
					{
						try
						{
							db.Product product = context.Product.Single(x => x.Id == id);

							HttpPostedFile file = Request.Files["file"];
							if (file.ContentLength > 0)
							{
								Common.SaveThumbnail(file.InputStream, product.Pictures.OrderBy(x => x.Id).First().GUID);
							}
							product.Name = Request.Form["name"].Trim();
							product.Type = int.Parse(Request.Form["type"]);
							product.Description = Request.Form["intro"].Trim();

							context.SubmitChanges();
							Response.Redirect("products.aspx?start=" + start + "&limit=" + limit, true);
						} catch (Exception ex)
						{
							Response.Write(ex.Message);
						}
					} else
					{
						try
						{
							string guid = Guid.NewGuid().ToString();
							HttpPostedFile file = Request.Files["file"];
							if (file.ContentLength > 0)
							{
								Common.SaveThumbnail(file.InputStream, guid);
							}
							db.Product product = new db.Product
							{
								Name = Request.Form["name"].Trim(),
								Type = int.Parse(Request.Form["type"]),
								Description = Request.Form["intro"].Trim(),
								GUID = guid
							};
							product.Pictures.Add(new db.ProductPictures { GUID = guid, IsCover = true });
							context.Product.InsertOnSubmit(product);
							context.SubmitChanges();

							Response.Redirect("products.aspx", true);
						} catch (Exception ex)
						{
							Response.Write(ex.Message);
						}
					}
				}

				if (isdelete)
				{
					db.Product product = context.Product.Single(x => x.Id == id);
					try
					{
						product.Pictures.ToList().ForEach(x => {
							File.Delete(Server.MapPath("~/App_Data/Products/" + x.GUID));
							File.Delete(Server.MapPath("~/upload/products/" + x.GUID + ".jpg"));
						});
						context.ProductPictures.DeleteAllOnSubmit(product.Pictures);
						context.Product.DeleteOnSubmit(product);
						context.SubmitChanges();
					} catch (Exception ex)
					{
						Response.Write(ex.Message);
					}
					Response.Redirect(Request.UrlReferrer.OriginalString, true);
				}

				p_list.DataSource = context.Product.Skip(Common.Start).Take(p_pager.Limit);
				p_list.DataBind();

				p_pager.Total = context.Product.Count();
			}
		}
	}
}